﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;

using FrameWork;
using FrameWork.Components;
using FrameWork.WebControls;


namespace FrameWork.web.Module.ITMS.Upload.News
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HeadMenuWebControls1.HeadOPTxt = TitleName_Config + "列表";
            HeadMenuWebControls1.HeadTitleTxt = TitleName_Config;
            HeadMenuWebControls1.ButtonList[0].ButtonName = TitleName_Config;
            TabOptionItem1.Tab_Name = TitleName_Config + "列表";

            if (!Page.IsPostBack)
            {
                OnStart();
                BindDataList();
            }
        }

        private void OnStart()
        {


            string[] A_Cats = A_Cats_Config.Split(',');
            foreach (string var in A_Cats)
            {
                ListItem li = new ListItem();
                li.Text = BusinessFacade.sys_FieldValueDisp(Convert.ToInt32(var)).V_Text;
                li.Value = var;
                A_Cat.Items.Add(li);
            }

            if (A_ID_Config + "" == "")
            {
                GridView1.Columns[0].Visible = false;
            }
            else
            {
                GridView1.Columns[0].HeaderText = A_ID_Config;
            }

            if (A_CatsName_Config == "")
            {
                GridView1.Columns[1].Visible = false;
                A_CatsNameSearch.Visible = false;
            }
            else
            {
                GridView1.Columns[1].HeaderText = A_CatsName_Config;
                A_CatsNameSearchTxt.Text = GridView1.Columns[1].HeaderText;
            }

            if (A_Title_Config == "")
            {
                GridView1.Columns[2].Visible = false;
                A_TitleSearch.Visible = false;
            }
            else
            {
                GridView1.Columns[2].HeaderText = A_Title_Config;
                A_TitleSearchTxt.Text = GridView1.Columns[2].HeaderText;
            }


            if (A_TitleSub_Config == "")
            {
                GridView1.Columns[3].Visible = false;
                A_TitleSubSearch.Visible = false;
            }
            else
            {
                GridView1.Columns[3].HeaderText = A_TitleSub_Config;
                A_TitleSubSearchTxt.Text = GridView1.Columns[3].HeaderText;
            }

            if (A_DateTime_Config == "")
            {
                GridView1.Columns[4].Visible = false;
                A_DateTimeSearch.Visible = false;
            }
            else
            {
                GridView1.Columns[4].HeaderText = A_DateTime_Config;
                A_DateTimeSearchTxt.Text = GridView1.Columns[4].HeaderText;
            }

            if (A_Photo_Config == "")
            {
                A_PhotoSearch.Visible = false;
            }
            else
            {
                A_PhotoSearchTxt.Text = A_Photo_Config;
            }

            if (A_Txt_Config == "")
            {
                A_TxtSearch.Visible = false;
            }
            else
            {
                A_TxtSearchTxt.Text = A_Txt_Config;
            }




        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindDataList()
        {
            QueryParam qp = new QueryParam();
            qp.Where = SearchTerms;
            qp.OrderType = 1;
            qp.Orderfld = "A_DateTime";
            qp.PageIndex = AspNetPager1.CurrentPageIndex;
            qp.PageSize = AspNetPager1.PageSize;
            int RecordCount = 0;
            ArrayList lst = BusinessFacade.FRAMEWORK_ARTICLEList(qp, out RecordCount);
            GridView1.DataSource = lst;
            GridView1.DataBind();
            this.AspNetPager1.RecordCount = RecordCount;
        }

        /// <summary>
        /// 点击分页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindDataList();
        }

        /// <summary>
        /// 查询条件
        /// </summary>
        private string SearchTerms
        {
            get
            {
                if (ViewState["SearchTerms"] == null)
                    ViewState["SearchTerms"] = string.Format(" Where A_Cat in ({0}) ", A_Cats_Config);
                return (string)ViewState["SearchTerms"];
            }
            set { ViewState["SearchTerms"] = value; }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            string A_Cat_Value = (string)Common.sink(A_Cat.UniqueID, MethodType.Post, 10, 0, DataType.Str);
            string A_Title_Value = (string)Common.sink(A_Title.UniqueID, MethodType.Post, 255, 0, DataType.Str);
            string A_TitleSub_Value = (string)Common.sink(A_TitleSub.UniqueID, MethodType.Post, 255, 0, DataType.Str);
            string A_Photo_Value = (string)Common.sink(A_Photo.UniqueID, MethodType.Post, 255, 0, DataType.Str);
            string A_Txt_Value = (string)Common.sink(A_Txt.UniqueID, MethodType.Post, 0, 0, DataType.Str);

            DateTime S_E_DateTime_Value = (DateTime)Common.sink(S_E_DateTime.UniqueID, MethodType.Post, 255, 0, DataType.Dat);
            DateTime E_E_DateTime_Value = (DateTime)Common.sink(E_E_DateTime.UniqueID, MethodType.Post, 255, 0, DataType.Dat);

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" Where A_Cat in ({0}) ", A_Cats_Config);

            if (A_Cat_Value != string.Empty)
            {
                sb.AppendFormat(" AND A_Cat = {0} ", Convert.ToInt32(A_Cat_Value));
            }

            if (A_Title_Value != string.Empty)
            {
                sb.AppendFormat(" AND A_Title like '%{0}%' ", Common.inSQL(A_Title_Value));
            }

            if (A_TitleSub_Value != string.Empty)
            {
                sb.AppendFormat(" AND A_TitleSub like '%{0}%' ", Common.inSQL(A_TitleSub_Value));
            }

            if (A_Photo_Value != string.Empty)
            {
                if (A_Photo_Value == "0")
                    sb.Append(" AND a_photo is null and A_Photo='' ");
                else
                    sb.Append(" AND A_Photo <>'' and a_photo is not null");
            }

            if (A_Txt_Value != string.Empty)
            {
                sb.AppendFormat(" AND A_Txt like '%{0}%' ", Common.inSQL(A_Txt_Value));
            }

            if (S_E_DateTime_Value != DateTime.MaxValue && E_E_DateTime_Value != DateTime.MaxValue)
            {
                if (Common.GetDBType == "Access")
                    sb.AppendFormat(" and A_DateTime between #{0} 00:00:00# and #{1} 23:59:59# ", S_E_DateTime_Value.Date.ToShortDateString(), E_E_DateTime_Value.Date.ToShortDateString());
                else if (Common.GetDBType == "Oracle")
                    sb.AppendFormat(" and A_DateTime between to_date('{0} 00:00:00','yyyy-mm-dd HH24:MI:SS') and to_date('{1} 23:59:59','yyyy-mm-dd HH24:MI:SS') ", S_E_DateTime_Value.Date.ToShortDateString(), E_E_DateTime_Value.Date.ToShortDateString());
                else
                    sb.AppendFormat(" and A_DateTime between '{0} 00:00:00' and '{1} 23:59:59' ", S_E_DateTime_Value.Date.ToShortDateString(), E_E_DateTime_Value.Date.ToShortDateString());

            }

            ViewState["SearchTerms"] = sb.ToString();
            BindDataList();
            TabOptionWebControls1.SelectIndex = 0;
        }

        private string TitleName_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["TitleName"].ToString() + "";
            }
        }

        private string A_ID_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_ID"] + "";
            }
        }

        private string A_Cats_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_Cats"] + "";
            }
        }

        private string A_CatsName_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_CatsName"] + "";
            }
        }

        private string A_Title_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_Title"] + "";
            }
        }
        private string A_TitleSub_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_TitleSub"] + "";
            }
        }
        private string A_Photo_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_Photo"] + "";
            }
        }

        private string A_Txt_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_Txt"] + "";
            }
        }
        private string A_DateTime_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_DateTime"] + "";
            }
        }
    }
}
